package dragon.core.page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;

import dragon.core.db.ColumnMeta;
import dragon.core.util.StringUtils;

/**
 * 数据分页
 */
public class Page {

	/**
	 * 记录总数
	 */
	private int totalItems;

	/**
	 * 每页显示数据条数
	 */
	private int itemsPerPage = 15;

	/**
	 * 页面总数
	 */
	private int totalPages;

	/**
	 * 当前页码
	 */
	private int currentPage = 1;

	/**
	 * 页面跳转的指定页码
	 */
	private int targetPage = -1;

	/**
	 * 翻页类型
	 */
	private PageAction pageAction = PageAction.NONE;

	/**
	 * 记录集起始位置
	 */
	private int firstIndex = 0;

	/**
	 * 记录集结束位置
	 */
	private int lastIndex = -1;

	/**
	 * 当前页面记录条数
	 */
	private int currentPageItems = 0;

	/**
	 * sql语句
	 */
	private String sql;

	/**
	 * 字段元信息
	 */
	ColumnMeta[] cols;

	/**
	 * 结果集
	 */
	List<Map<String, Object>> result;

	public Page() {
		calculateIndex();
	}

	/**
	 * 计算数据起始和结束索引 <br>
	 * 查询记录集之前需要先执行此方法
	 */
	public void calculateIndex() {

		// 默认每页显示记录条数
		if (itemsPerPage < 0) {
			itemsPerPage = 15;
		}

		// 根据记录总数和每页显示数据条数计算总页数
		totalPages = totalItems / itemsPerPage + (totalItems % itemsPerPage > 0 ? 1 : 0);

		if (totalPages == 0) {
			totalPages = 1;
		}

		// 判断跳页
		if (targetPage != -1) {
			currentPage = targetPage;
		}

		// 判断翻页
		if (pageAction.compareTo(PageAction.FIRST) == 0) {
			// 首页
			currentPage = 1;
		} else if (pageAction.compareTo(PageAction.PREVIOUS) == 0) {
			// 上一页
			currentPage -= 1;
		} else if (pageAction.compareTo(PageAction.NEXT) == 0) {
			// 下一页
			currentPage += 1;
		} else if (pageAction.compareTo(PageAction.LAST) == 0) {
			// 尾页
			currentPage = totalPages;
		} else if (pageAction.compareTo(PageAction.ALL) == 0) {
			// 显示所有数据
			totalPages = 1;
			currentPage = totalPages;
			itemsPerPage = totalItems;
		}

		if (pageAction.compareTo(PageAction.NONE) != 0) {
			pageAction = PageAction.NONE;// 翻页之后，需要复位翻页状态
			targetPage = -1;// 如果存在翻页，则复位跳页状态
		}

		// 判断当前页
		if (currentPage > totalPages) {
			currentPage = totalPages;

			if (targetPage != -1) {
				targetPage = currentPage;
			}
		}

		firstIndex = (currentPage - 1) * itemsPerPage;
		lastIndex = currentPage * itemsPerPage - 1;

		if (currentPage < totalPages) {
			currentPageItems = itemsPerPage;
		} else {
			currentPageItems = totalItems - (currentPage - 1) * itemsPerPage;
		}
	}

	@Override
	public String toString() {
		return String.format("\nPageInfo\ntotalItems:%d\nitemsPerPage:%d\ntotalPages:%d\ncurrentPage:%d\ntargetPage:%d\npageAction:%s\nfirstIndex:%d\nlastIndex:%d\nSQL:%s\n\n", totalItems,
				itemsPerPage, totalPages, currentPage, targetPage, pageAction, firstIndex, lastIndex, sql);
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		if (StringUtils.isInteger(currentPage)) {
			this.currentPage = StringUtils.intValue(currentPage);
		}
	}

	public int getItemsPerPage() {
		return itemsPerPage;
	}

	public void setItemsPerPage(String itemsPerPage) {
		if (StringUtils.isInteger(itemsPerPage)) {
			this.itemsPerPage = StringUtils.intValue(itemsPerPage);
		}
	}

	public int getTargetPage() {
		return targetPage;
	}

	public void setTargetPage(String targetPage) {
		if (StringUtils.isInteger(targetPage)) {
			this.targetPage = StringUtils.intValue(targetPage);
		}
	}

	public int getTotalItems() {
		return totalItems;
	}

	public void setTotalItems(int totalItems) {
		this.totalItems = totalItems;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public int getFirstIndex() {
		return firstIndex;
	}

	public int getLastIndex() {
		return lastIndex;
	}

	public PageAction getPageAction() {
		return pageAction;
	}

	public void setPageAction(String pageAction) {
		if (!StringUtils.isEmpty(pageAction)) {
			this.pageAction = PageAction.valueOf(pageAction);
		}
	}

	public void setItemsPerPage(int itemsPerPage) {
		this.itemsPerPage = itemsPerPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public void setPageAction(PageAction pageAction) {
		this.pageAction = pageAction;
	}

	public void setTargetPage(int targetPage) {
		this.targetPage = targetPage;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public int getCurrentPageItems() {
		return currentPageItems;
	}

	public ColumnMeta[] getCols() {
		return cols;
	}

	public void setCols(ColumnMeta[] cols) {
		this.cols = cols;
	}

	public List<Map<String, Object>> getResult() {
		return result;
	}

	public void setResult(List<Map<String, Object>> result) {
		this.result = result;
	}

	@SuppressWarnings("unchecked")
	public String getJsonMapResult() {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", getTotalItems());
		jsonMap.put("rows", getResult());
		JSONObject jsonObject = new JSONObject();
		jsonObject.putAll(jsonMap);
		return jsonObject.toJSONString();
	}
}
